package fr.cephb.joperon.search;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.logging.Logger;

import org.w3c.dom.Element;

import com.sleepycat.db.DatabaseException;

import fr.cephb.joperon.core.Operon;
import fr.cephb.joperon.core.db.DTermsDB;
import fr.cephb.joperon.core.db.EWordsDB;
import fr.cephb.joperon.core.db.ITermsDB;
import fr.cephb.joperon.core.db.LTermsDB;
import fr.cephb.joperon.core.db.SemNetDB;
import fr.cephb.joperon.core.db.UTermsDB;
import fr.cephb.joperon.core.db.XFrameDB;
import fr.cephb.joperon.core.db.XTermsDB;
import fr.cephb.joperon.core.entities.EWords;
import fr.cephb.joperon.core.entities.SemNet;
import fr.cephb.joperon.core.entities.XFrame;
import fr.cephb.joperon.core.entities.XTerms;
import fr.cephb.joperon.core.iterator.DatabaseValueIterator;
import fr.cephb.joperon.tool.OperonFactory;
import fr.cephb.joperon.tool.XMLFactory;
import fr.cephb.lang.UInt32;

/**
 * SearchOperon
 * @author lindenb
 *
 */
public class SearchOperon
	extends OperonFactory
	{
	private static Logger LOG= Logger.getLogger(SearchOperon.class.getCanonicalName());

	
	private SearchOperon(Operon operon)
		{
		super(operon);
		}
	
	
	
	private void debug(Object o)
		{
		System.err.println(o);
		}
	
	public void search(String term) throws DatabaseException
		{
		if(term==null) return;
		term=term.trim();
		if(term.length()==0) return;
		debug("Searching for "+term+" in UTerms");
		
		/* Open uTerms to read */
		/* uterms db extends OperonDBSingleValue.STRING<UTerms> */
		Integer tid =getUTerms().getTermId(term);
		
		// If found save the term ID
		if(tid!=null)
			{
			searchTid(tid);
			return;
			}
		// Else open lTerms to read
		debug("could not find query \""+ term+"\" in UTERMS. Searching in LTerms");
		tid= getLTerms().getTermId(term);
		debug("lterm="+tid);
		if(tid!=null)
			{
			searchTid(tid);
			return;
			}
		
		debug("could not find query \""+ term+"\" in LTerms. Searching in EWords");
		for(EWords ewWords: getEWords().getValues())
			{
			debug("Found an E-Words "+ewWords);
			searchTid(ewWords.getTid());
			}

		debug("Done. Searching "+term);
		debug("\n===================================\n\n\n");
		}
	
	
	private void searchTid(int tid)throws DatabaseException
		{
		
		debug("Get term definition from dTerms (a term has 0 or 1 definition)");
		String definition =  getDTerms().getDefinition(tid);
		if(definition!=null)
			{
			debug("definition of "+tid+" = "+definition);
			}
		else
			{
			debug("no definition found for "+tid+" = "+definition);
			}
		//debug("Looking in ITerms to get the name of tid["+tid+"] and found "+getITermsDb().getTermName(tid));
		
		debug("Searching for TID="+tid+" in XTerms.");
		
		
		
		DatabaseValueIterator<XTerms> iter=getXTerms().listValues(tid);
		XMLFactory factory= new XMLFactory(this);
		Element root= factory.createElement("xterm-list");
		factory.getDocument().appendChild(root);
		
		
		while(iter.hasNext())
			{
			XTerms t= iter.next();
			debug("========== found XTerm "+t);
			root.appendChild(factory.createXTerms(t));
			if(t.getFrame()==0)
				{
				debug("xterm.frame==0. continue");
				continue;
				}
			
			
			
			int count=0;
			DatabaseValueIterator<XFrame> iter2=getXFrame_XTHING().listValues(t.getFrame());
			while(iter2.hasNext())
				{
				XFrame xframe=iter2.next();
				
				++count;
				if(count<3) debug("========== found XFrame\n"+xframe);
				}
			iter2.close();		
			debug("found count xframe "+count);
			}
		iter.close();
		
		try {
			factory.print(System.out);
		} catch (Exception e) {
			e.printStackTrace();
		}
		}
	
	
	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args)
		{
		try {
			Operon operon= Operon.newInstance();
			SearchOperon app= new SearchOperon(operon);
			//app.search("ADH5");
			app.search("Roxan");
			//app.search("EIF4G");
			//app.search("(d)J");
			app.close();
			operon.close();
			}
		catch (Exception e)
			{
			e.printStackTrace();
			}

		}

}
